// import the App class
import timber.controller.App;


/**
	Generic page controller class.
	
	Registers for navigation events from the App and
	if the target of the navigation is somewhere else,
	hide's itself otherwise, shows itself.
	@see App.

	@usage
	EXAMPLE COMMON USAGE EXAMPLES
	These example subclasses show how to solve to common problems using this class	
	
	1) A page that uses and XML file for configuration, and an external SWF for display.

		<code>
		import timber.data.XMLData;
		import timber.controller.CSS;

		dynamic class ExternalXMLPage extends timber.controller.Page
		{
			private var thedata;
	
			// assumes content_mc is an instance of timber.LoadView
			public function render() {
		
				// when the section has loaded, set all the data
				var self = this;
				this.content_mc.onLoadInit = function() {
					// title
					this.img_mc.title_txt.text = self.thedata.selectSingleNode('/page/title/text()');

					// content
					this.img_mc.content_txt.htmlText = self.thedata.selectSingleNode('/page/content/node()[2]').nodeValue;
			
					// main image
					this.img_mc.image_mc.load(self.thedata.selectSingleNode('/page/@image').nodeValue);
			
					self.show();
				}

				// load the data, when it's loaded, load the section swf
				this._visible = true;
				thedata = new XMLData(
					'path/to/xml', 
					function() { self.content_mc.load('content.swf'); }
				);
			}
		}
		</code>

	2) A page that uses it's name as the id. If the first element in the path is the clip's name
		it will display, if the path has more elements it will turn them into a path to a nested
		clip and display it.
		
		<code>
		class EasyPage extends timber.controller.StaticPage
		{
	
			public function EasyPage() {}

	
			// see if the page should show
			public function pageShouldDisplay(path) {
				// if the first item is me, show
				// and if there are more elements, show them as nested clips
				if (path[0] == getID()) {
					for (var p in this) {
						if (this[p] instanceof MovieClip) this[p]._visible = false;
					}

					if (path.length > 1) {
						eval(path.join('.'))._visible = true;
					}
					return true;
				}
		
				return false;
			}


			public function getID() {
				return this._name;
			}
		}
		</code>



	@author Chandler McWilliams
	@version 2005-06-29
*/
class timber.controller.StaticPage extends MovieClip
{
	private var id;
	private var hasRendered;
	
// ===========================================================
// - CONSTRUCTOR
// ===========================================================
	public function StaticPage() {
		hide();

		// add app listener
		App.addListener(this);
	}


// ===========================================================
// - SHOW/HIDE
// ===========================================================
	/**
		For the listener, called when a navigation event is sent.
		
		@param path array.
	**/
	public function onNavigate(path) {
		pageShouldDisplay(path)?$show(path):$hide(path);
	}
	
	/**
		Determine if the page should be displayed given the targeted path.
		
		@param array path.
		@return boolean Should the page be displayed.
	**/
	public function pageShouldDisplay(path) {
		return (path.join('/') == getID());
	}

	/**
		Do the work of showing the page beyond toggling visibility
		typically this would only be called once the first time the
		page is shown.

	**/
	public function render() {
		show();
	}
	
	
	
	/**
		Private show and hide to make sure internal states are properly updated
	**/
	private function $show(path) {
		if (!hasRendered) {
			render(path);
			hasRendered = true;
		} else {
			show(path);
		}
	}
	
	private function $hide(path) {hide(path);}
	
	/**
		Called if the target of navigation is this page.
		If the page hasn't been rendered, then render it.
		Subclasses can override for fancy effects.

	**/
	public function show(path) {
		this._visible = true;
	}

	/**
		Called if the target of navigation is not this page.
		Subclasses can override for fancy effects.

	**/
	public function hide(path) {
		this._visible = false;
	}


// ===========================================================
// - ACCESSORS
// ===========================================================
	/**
		Sets ID
		
		@return void.
	**/
	public function setID(newid) {
		this.id = newid;
	}

	/**
		Returns the id for this page.

		@return mixed id.
	**/
	public function getID() {
		return this.id;
	}


}